<template>
  <div class="login">
    <!-- 表单验证
    el-form
       model
       rules
       ref
    el-form-item
       prop:需要验证的字段名
    rules:{
      prop的值:[
      {
        required:true,message:'提示信息',trigger:"blur/change"
      },{
        min:最小长度,max:最大长度,message:"错误信息",trigger:"blur/change"
      },{
        validator:(rule,value,callback)=>{
          callback():验证成功
          callback(new Error(错误信息)):验证失败
        },trigger:"blur/change"
      }
      ]
    }
    全局验证
    this.$refs.ref值.validate(result=>{
      result:true验证通过，false:验证失败
    })
    局部验证
    this.$refs.ref值.validateField([需要验证的prop的值]，(errorMessage)=>{
      errorMessage:验证通过它的值为空，不通过就是有值
    })
     -->
    <el-form class="box" :model="form" :rules="rules" ref="form">
      <div class="logo">
        <!-- <h3>{{ num }}</h3>
        <button @click="setNum">点击</button> -->
        <img src="@/assets/common/login-logo.png" alt="" />
      </div>
      <el-form-item prop="mobile">
        <el-input v-model="form.mobile"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="form.password" type="password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" class="btn" @click="submit">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
// import { login } from '@/api/user.js'
// import MixinCom from ''
export default {
  // mixins: [MixinCom],
  data () {
    return {
      num: 999,
      form: {
        mobile: '13800000002', //	string	必须		手机号
        password: '123456' //	string	必须		密码
      },
      rules: {
        mobile: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              const reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('请正确填写手机号'))
              }
            }
          }
        ], //	string	必须		手机号
        password: [
          { required: true, message: '必填', trigger: 'change' },
          {
            min: 6,
            max: 12,
            message: '请输入六至十二位密码',
            trigger: 'change'
          }
        ] //	string	必须		密码
      }
    }
  },
  created () {
    console.log('login created')
  },
  methods: {
    submit () {
      this.$refs.form.validate(async result => {
        if (result) {
          await this.$store.dispatch('user/toLogin', this.form)
          this.$message.success('登录成功')
          // 获取路由回跳地址参数
          const redirect = this.$route.query.redirect || '/' // redirect无值就取/(回首页)
          this.$router.push(redirect)
          // const res = await login(this.form)
          // console.log(res)
          // this.$message.success('验证成功')
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  background: url(~@/assets/common/login.jpg);
  display: flex;
  justify-content: center;
  align-items: center;
  .box {
    width: 520px;
    height: 400px;
    .logo {
      text-align: center;
      padding-bottom: 20px;
    }
    .btn {
      width: 100%;
      padding: 15px;
      background-color: #407ffe;
      border: none;
    }
  }
}
</style>
